package org.kjk.employee;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class EmployeeSearchController extends AbstractController {
	private EmployeeDAOImpl dao;

	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("Call Employee search");
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");

		String sql = null;
		List<EmployeeDTO> result = null;
		String[] param = null;
		if (queryField == null) {
			sql = "select * from employees";			
		} else if (queryField.length() != 0 && queryType.equals("1")) {
			sql = "select * from employees where LAST_NAME = ?";
			param = new String[] { queryField };
		} else if (queryField.length() != 0 && queryType.equals("2")) {
			sql = "select * from employees where EMPLOYEE_ID = ?";
			param = new String[] { queryField };
		} else {
			sql = "select * from employees";
		}

		result = (List<EmployeeDTO>) dao.select(sql, param);
		return new ModelAndView("jsp/employeeSearchResult.jsp", "result",
				result);
	}

	public void setDao(EmployeeDAOImpl dao) {
		this.dao = dao;
	}
}
